﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MapController : MonoBehaviour
{
    
    private BaseLaunch launch;


    Vector3 mouseDownPosition;

  
 
    //
    //    int touchStep=0;
    //
    //
    //    public float stepMinDuration=0.2f;
    //
    //
    //    float stepCounter;


    public bool setGoal;

    void Start()
    {

        launch = GetComponentInParent<BaseLaunch>();

       
    }


    public void SetGoal()
    {

        setGoal = true;
    }

    void Update()
    {
		



        if (EventSystem.current.IsPointerOverGameObject())
        {

            return;

        }


        if (Input.touchCount == 2)
        {




            Touch touchZero = Input.GetTouch(0);

            Touch touchOne = Input.GetTouch(1);


            if (touchZero.phase == TouchPhase.Moved && touchOne.phase == TouchPhase.Moved)
            {


                float distance = Vector2.Distance(touchOne.position - touchOne.deltaPosition, touchZero.position - touchZero.deltaPosition) - Vector2.Distance(touchOne.position, touchZero.position);


                float angle = Vector2.Angle(touchZero.deltaPosition, touchOne.deltaPosition);

                if (Mathf.Abs(angle) < 30.0f)
                {

                    Vector3 detlaPosition = Camera.main.ScreenToWorldPoint(touchZero.position) - Camera.main.ScreenToWorldPoint(touchZero.position - touchZero.deltaPosition);

                    detlaPosition.z = 0;

                    Camera.main.transform.position -= detlaPosition * 4.0f;


                }
                else
                {

                    Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize + distance / 100f, 1, 6);

                }







            }


//
//            touchStep=0;
//
//            stepCounter=0;


            setGoal = false;

            return;


        }



        bool mouseClick = Input.GetMouseButtonDown(0);

        bool touchClick = false;

       

        if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began && setGoal == true)
        {
            
            touchClick = true;


        }

//        if (touchStep==0)
//        {
//
//            if (Input.touchCount==0)
//            {
//
//
//                stepCounter=Mathf.Clamp(stepCounter+Time.deltaTime,0f,stepMinDuration+0.01f);
//
//
//                if (stepCounter>stepMinDuration)
//                {
//
//                    stepCounter=0f;
//
//                    touchStep=1;
//
//                }
//
//
//            }
//
//
//        }else if (touchStep==1)
//        {
//            if (Input.touchCount==1 && Input.GetTouch(0).phase==TouchPhase.Ended)
//            {
//
//
//                touchStep=2;
//
//            }
//
//
//        }
//
//        else if (touchStep==2)
//        {
//
//
//            stepCounter=Mathf.Clamp(stepCounter+Time.deltaTime,0f,stepMinDuration+0.1f);
//
//
//            if (stepCounter>stepMinDuration)
//            {
//
//                stepCounter=0f;
//
//                touchStep=0;
//
//               
//
//            }
//
//        }



        if ((touchClick || mouseClick) && setGoal)
        {
            
            Vector2 touchPosition;

            if (touchClick)
            {

                touchPosition = Input.GetTouch(0).position;

            }
            else
            {

                touchPosition = Input.mousePosition;
            }
            
             
                
            Vector3 worldPosition = Camera.main.ScreenToWorldPoint(touchPosition);

            launch.OnClickMap(transform.parent.InverseTransformPoint(worldPosition));

            setGoal = false;
            return;


        }


        if (Input.GetMouseButtonDown(2))
        {
            
            mouseDownPosition = Input.mousePosition;

            return;

        }


        if (Input.GetMouseButton(2))
        {

            Vector3 detlaPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.ScreenToWorldPoint(mouseDownPosition);

            mouseDownPosition = Input.mousePosition;

            detlaPosition.z = 0;

            Camera.main.transform.position -= detlaPosition;

            return;

        }



        if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
        {
                
            Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize - 0.5f, 1);

            return;
        }

        if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
        {
            Camera.main.orthographicSize = Mathf.Min(Camera.main.orthographicSize + 0.5f, 6);

            return;

        }


    }



    public void AimCameraToRobot()
    {


        GameObject robot = GameObject.Find("Robot");

        if (robot != null)
        {

            Vector3 pos = Camera.main.transform.position;



            pos.x = robot.transform.position.x;

            pos.y = robot.transform.position.y;


            Camera.main.transform.position = pos;

        }

    }


}
